//
//  SAShopAllocateHeaderView.m
//  saasRetail
//
//  Created by 黄志刚 on 2022/4/19.
//

#import "SAShopAllocateHeaderView.h"
#import "SASalesOrderViewListModel.h"

@interface SAShopAllocateHeaderCell : UITableViewCell

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UIImageView *imageV;
@property (nonatomic, strong) UIButton *addBtn;
@property (nonatomic, strong) SASalesOrderViewListModel *model;
@property (nonatomic, copy) void (^imageBlock2)(void);
@property (nonatomic, copy) void (^addBlock2)(void);

@end

@implementation SAShopAllocateHeaderCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

-(void)setModel:(SASalesOrderViewListModel *)model {
    _model = model;
    self.titleLab.text = [NSString stringWithFormat:@"%@,%@",model.productCode,model.productName];
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"sale_bule_defaul"]];
}

#pragma mark - event
-(void)clickedTab {
    !self.imageBlock2 ?: self.imageBlock2();
}

-(void)clickedAddBtn {
    !self.addBlock2 ?: self.addBlock2();
}

-(void)initUI {
    self.contentView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.imageV];
    [self.contentView addSubview:self.addBtn];
}

-(UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithFrame:SARectMake(65, 0, 300, 40)];
        _titleLab.textColor = kBlackColor;
        _titleLab.font = kFont(16);
    }
    return _titleLab;
}

-(UIImageView *)imageV {
    if (!_imageV) {
        _imageV = [[UIImageView alloc] initWithFrame:SARectMake(20, 3, 34, 34)];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickedTab)];
        [_imageV addGestureRecognizer:tap];
        _imageV.userInteractionEnabled = YES;
        _imageV.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _imageV;
}

-(UIButton *)addBtn {
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _addBtn.frame = SARectMake(65, 0, kBaseScreenWidth - 65, 40);
        [_addBtn addTarget:self action:@selector(clickedAddBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}

@end

@interface SAShopAllocateHeaderView()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView2;

@end

@implementation SAShopAllocateHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        [self initUI];
    }
    return self;
}

-(void)setModel:(SASalesOrderViewListModel *)model {
    _model = model;
    [self.tableView2 reloadData];
}

#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WeakSelf
    SAShopAllocateHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAShopAllocateHeaderCell" forIndexPath:indexPath];
    cell.model = self.model;
    cell.imageBlock2 = ^{
        !weakSelf.imageBlock ?: weakSelf.imageBlock();
    };
    cell.addBlock2 = ^{
        !weakSelf.addBlock ?: weakSelf.addBlock();
    };
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
  
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
     if (editingStyle == UITableViewCellEditingStyleDelete) {
         !self.deleteBlock ?: self.deleteBlock();
     }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
        return @"删除整款";
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(40);
}

#pragma mark - initUI
-(void)initUI {
    [self.contentView addSubview:self.tableView];
}

#pragma mark - lazy
-(UITableView *)tableView {
    if (!_tableView2) {
        _tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScaleNum(40)) style:UITableViewStylePlain];
        if (@available(iOS 15.0, *)) {
            _tableView2.sectionHeaderTopPadding = 0;
        }
        _tableView2.backgroundColor = [UIColor whiteColor];
        _tableView2.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView2.delegate = self;
        _tableView2.dataSource = self;
        _tableView2.estimatedRowHeight = 0;
        _tableView2.estimatedSectionHeaderHeight = 0;
        _tableView2.estimatedSectionFooterHeight = 0;
        _tableView2.bounces = NO;
        [_tableView2 registerClass:[SAShopAllocateHeaderCell class] forCellReuseIdentifier:@"SAShopAllocateHeaderCell"];
    }
    return _tableView2;
}

@end
